package com.egov.wrdmp.reporting.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.egov.wrdmp.reporting.model.Expense;
import com.egov.wrdmp.reporting.service.ExpenseService;


@Controller
@RequestMapping("/expenseservice")
public class ExpensesController {
	@Autowired
    public ExpenseService expenseService;
	
	
	 	@RequestMapping(value = "/listexpense", method = RequestMethod.GET)
	 	@ResponseBody
	    public List listExpense(Model model) {
	    	model.addAttribute("report", new Expense());
	    	model.addAttribute("reportList", expenseService.listExpense());
	        for(int i=0;i<expenseService.listExpense().size();i++){
	        	Expense tmpExpense = expenseService.listExpense().get(i);
	        	System.out.println("Expense Name is "+tmpExpense.getExpenseName());
	        }
	        
	        return expenseService.listExpense();
	    }
	 
	 	@RequestMapping(value = "/addexpense/{expensename}/{expensecategory}/{amount}", method = RequestMethod.POST)
	 	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	    public String addExpense(@PathVariable("expensename") String expensename, @PathVariable("expensecategory") String expensecategory, @PathVariable("amount") Float amount) {
	 
	 		Expense expense = new Expense();
	 		expense.setExpenseName(expensename);
	 		expense.setExpenseCategory(expensecategory);
	 		expense.setAmount(amount);
	    	expenseService.addExpense(expense);
	 
	        return "redirect:/expenseservice/list";
	    }
	 
	 	@RequestMapping(value = "/deleteexpense/{id}", method = RequestMethod.DELETE)
	 	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	 	@ResponseBody
	    public String deleteContact(@PathVariable("expenseId")
	    Integer expenseId) {
	 
	    	expenseService.removeExpense(expenseId);
	 
	        return "redirect:/expenseservice/list";
	    }
 
}
